﻿/*
 * 北京合欣机电直流屏
 * changli.xiao
 * 2014/1/18
 * 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JN.Utility;
using System.IO.Ports;
using LogManager;
using System.Threading;
using Power.Common;

namespace Power.EPlate
{
    public class DCPanelController
    {
        #region property
        public delegate void UpdateStatusHandler(MsgType msgType, object msg);
        public static event UpdateStatusHandler UpdateStatus;
        private Thread readThread;
        private object lockRead = new object();
        private Manager.SystemStatus sysStatus = Manager.SystemStatus.Instance;
        private MeterControl.MeterCommon portCommon = MeterControl.MeterCommon.Instance;
        private SerialPort readPort;
        private const string read_command = "010200000040";   //读取指令
        #endregion
        private static DCPanelController instance = null;
        private static readonly object plcLock = new object();
        public static DCPanelController Instance
        {
            get
            {
                lock (plcLock)
                {
                    if (instance == null)
                    {
                        instance = new DCPanelController();
                    }
                    return instance;
                }
            }
        }

        private DCPanelController()
        {
            OpenPort();
        }
        public bool OpenPort()
        {
            bool ret = false;
            try
            {
                readPort = new SerialPort();
                readPort.PortName = System.Configuration.ConfigurationManager.AppSettings.Get("DCPort");
                readPort.BaudRate = 9600;
                readPort.DataBits = 8;
                readPort.StopBits = StopBits.One;
                readPort.Parity = Parity.None;
                readPort.Open();
                ret = true;
                return ret;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return ret;
            }            
        }
        public bool Start()
        {
            sysStatus.IsStartDCRead = true;
            StartThread();
            return true;
        }
        public bool Stop()
        {
            sysStatus.IsStartDCRead = false;            
            return true;
        }
        private void StartThread()
        {
            if (readThread == null)
            {
                readThread = new Thread(new ThreadStart(ReadDC));
                readThread.Name = "DC Read Thread";
                readThread.IsBackground = true;
                readThread.Start();
            }
        }
        private void ReadDC()
        {
            while (true)
            {
                if (sysStatus.IsStartDCRead)
                {
                    ReadFun();
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
        private void ReadFun()
        {
            lock (lockRead)
            {
                DateTime startTime = DateTime.Now;
                try
                {
                    //1. read data
                    string revMsg = string.Empty;
                    bool ret = portCommon.Write(readPort, read_command, ref revMsg);
                    if (!ret)
                    {
                        Common.Notify.NotifyMessage.Show("错误", "读取直流屏数据错误!!");
                        return;
                    }
                    //2. analysis data

                    //end
                }
                catch (Exception ex)
                {
                    SystemLogger.Write(ex);
                }
                finally
                {
                    TimeSpan useTime = DateTime.Now - startTime;
                    SystemLogger.Write(LogType.Flow, "DC Panel One flow use time:" + useTime.TotalMilliseconds, "ReadFun");
                    //4. do sleep                     
                    Thread.Sleep(1000);
                }
            }
        }
    }
}
